package com.zigong.app.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.zigong.app.R;
import com.zigong.app.application.Constances;
import com.zigong.app.beans.VoteOption;
import com.zigong.app.ui.adapter.holder.VoteHolder;

import java.util.Arrays;
import java.util.List;

/**
 * Created by liuyu on 2017/8/17.
 */

public class VoteAdapter extends BaseHolderAbsAdapter<VoteOption, VoteHolder> {

    private int total;
    private int mode;
    private boolean hasVoted;

    private int selePos = -1;
    private SparseArray<String> seleArray;

    private String seleId;

    public VoteAdapter(Context context, List<VoteOption> list) {
        super(context, list);
        canSelector = true;
    }

    @Override
    public int getItemCount() {
//        if (data!=null){
//            int size=data.size();
//            if (size>5){
//                return 5;
//            }
//            return size;
//        }
//        return 0;
        return super.getItemCount();
    }

    @Override
    public VoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_vote, parent, false);
        VoteHolder vh = new VoteHolder(context, v);
        vh.setInternalCheckChangedListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(VoteHolder holder, int position) {
        VoteOption vo = data.get(position);
        holder.setDataBefor(total, mode, !hasVoted);
        super.onBindViewHolder(holder, position);
    }

    @Override
    protected boolean isSelected(VoteOption voteOption, int position) {
        if (mode == Constances.VOTE_TYPE_RADIO) {
            return selePos == position;
        } else if (mode == Constances.VOTE_TYPE_MULTI) {
            return seleArray.indexOfKey(position) >= 0;
        }
        return super.isSelected(voteOption, position);
    }

    @Override
    public void onItemInternalCheckedChanged(View itemView, View checkable, int postion, boolean checked) {
        VoteOption vo = getItem(postion);
        if (mode == Constances.VOTE_TYPE_RADIO) {
            if (checked) {
                seleId = vo.getVoteOptionId();
                int op = selePos;
                selePos = postion;
                if (op >= 0) {
                    notifyItemChanged(op);
                }
                selePos = postion;
            } else {
                seleId = null;
                selePos = -1;
            }
        } else if (mode == Constances.VOTE_TYPE_MULTI) {
            if (checked) {
                seleArray.put(postion, vo.getVoteOptionId());
            } else {
                seleArray.remove(postion);
            }
        }
    }

    public boolean hasSelector() {
        if (mode == Constances.VOTE_TYPE_RADIO) {
            return !TextUtils.isEmpty(seleId);
        } else if (mode == Constances.VOTE_TYPE_MULTI) {
            return seleArray != null && seleArray.size() > 0;
        }
        return false;
    }

    public void setState(int total, int mode, boolean hasVoted) {
        this.total = total;
        this.mode = mode;
        this.hasVoted = hasVoted;
        if (mode == Constances.VOTE_TYPE_RADIO) {
            seleArray = null;
        } else {
            seleArray = new SparseArray<>();
        }
    }

    public void setHasVoted(boolean hasVoted) {
        this.hasVoted = hasVoted;
    }

    public String getSelectIdsStr() {
        if (mode == Constances.VOTE_TYPE_RADIO) {
            return seleId;
        } else if (mode == Constances.VOTE_TYPE_MULTI) {
            String[] strs = getSelectIdArray();
            if (strs != null) {
                return Arrays.toString(strs);
            }
        }
        return null;
    }

    public String[] getSelectIdArray() {
        if (seleArray == null || seleArray.size() == 0) {
            return null;
        }
        if (seleArray.size() == 1) {
            return new String[]{seleArray.valueAt(0)};
        }
        String[] strs = new String[seleArray.size()];
        for (int i = 0; i < seleArray.size(); i++) {
            strs[i] = seleArray.valueAt(i);
        }
        return strs;
    }
}
